/*
 Copyright (c) 2009, Ivan Popivanov
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.

 * Neither the name of StockCandles nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CANDLESERIES_H_
#define CANDLESERIES_H_

#include <math.h>

#include <QtCore/QVector>
#include <QtCore/QString>

#include "Candle.h"

class CandleSeries : public QVector<Candle>
{
public:
   CandleSeries();
   CandleSeries(const QString &symbol);
   ~CandleSeries();

   void init(const QString &symbol) { this->symbol = symbol; resize(0); }

   bool loadFromYahooCsv(const QString &path, const QDateTime *start = NULL);
   bool storeToYahooCsv(const QString &path);

   // Creates a new CandleSeries of maximum length of maxLen (-1 means as long as possible).
   // Each candle in the new series is a summary of candlesPerCandle candles of the original.
   CandleSeries compress(uint candlesPerCandle, int maxLen = -1);

   CandleSeries dailyToWeekly()
   {
      return dailyTo('w');
   }

   CandleSeries dailyToMonthly()
   {
      return dailyTo('m');
   }

   CandleSeries dailySummarized(int numDays)
   {
      return dailyTo('d', numDays);
   }

   const QString &getSymbol() { return symbol; }
   void setSymbol(const QString &symbol) { this->symbol = symbol; }

   int findDateIndex(const QDateTime &date);

   // Returns the series as arrays. The most recent candle is last (in the series it's first). If any
   // of the pointers is not NULL, the function assumes that there is enough memory to hold the data.
   bool getRawData(double *&opens, double *&highs, double *&lows, double *&closes, uint *&volumes);

   // Returns a subset from the series as an array. The sample starts at index "first" of the original
   // series. Depending on "resolution" and "units", the function will skip certain number of points
   // before the next point. The function does not output more than cnt number of points.
   // The resulting sample is stored in the arrays from 0 (the oldest) to maxCnt (the newest). The
   // function returns the actual number of points output or -1 in case of an error.
   //
   // For example: 01/15/2001 01/16/2001 01/17/2001 01/18/2001 01/19/2001 01/20/2001 01/21/2001
   //    Resolution = 'd' (daily), units = 2 (sample ever second date), maxCnt = 3
   //    Result: 01/17/2001 01/19/2001 01/21/2001
   int getRawSample(double *&opens, double *&highs, double *&lows, double *&closes, uint *&volumes,
            int first, int resolution, int units, int maxCnt);

   CandleSeries dailyTo(char resolution, int numUnits = -1);

private:
   double round(double d) { return floor(d*10000) / 10000; }

private:
   QString symbol;
};

#endif /* CANDLESERIES_H_ */
